package com.android.base.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;

import com.example.lib_base.R;
import com.example.lib_base.databinding.LibBaseDialogCheckBinding;


/**
 * FileName: DialogLoading
 * Author: admin
 * Date: 2020/5/21 14:28
 * Description:
 */
public class DialogCheck extends Dialog {

    private Context context;
    private String title;
    private String checkContent;
    private String left;
    private String right;
    private LibBaseDialogCheckBinding checkBinding = null;
    private ActionLister clickLister;

    public DialogCheck(@NonNull Context c) {
        super(c, R.style.dialog_loading);
        this.context = c;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((Activity) context).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        checkBinding = LibBaseDialogCheckBinding.inflate(LayoutInflater.from(context));
        setContentView(checkBinding.getRoot());
        this.setCanceledOnTouchOutside(false);
        this.setCancelable(false);

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = getScreenWidth(context) / 5 * 4;
        getWindow().setAttributes(lp);

        if (TextUtils.isEmpty(title)) {
            checkBinding.tvTitle.setVisibility(View.GONE);
        } else {
            checkBinding.tvTitle.setText(title);
        }
        if (TextUtils.isEmpty(left)) {
            checkBinding.tvCancel.setText("取消");
        } else {
            checkBinding.tvCancel.setText(left);
        }

        if (TextUtils.isEmpty(right)) {
            checkBinding.tvEnter.setText("确定");
        } else {
            checkBinding.tvEnter.setText(right);
        }

        checkBinding.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (clickLister != null) {
                    clickLister.onCheckClick(isChecked);
                }
            }
        });

        checkBinding.tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (clickLister != null) {
                    clickLister.onLeftClick(DialogCheck.this);
                }
            }
        });

        checkBinding.tvEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (clickLister != null) {
                    clickLister.onRightClick(DialogCheck.this);
                }
            }
        });
    }

    public DialogCheck setTitle(String t) {
        this.title = t;
        return this;
    }

    public DialogCheck setCheckContent(String content) {
        this.checkContent = content;
        return this;
    }

    public DialogCheck setLeft(String l) {
        this.left = l;
        return this;
    }

    public DialogCheck setRight(String r) {
        this.right = r;
        return this;
    }

    public DialogCheck setActionLister(ActionLister lister) {
        this.clickLister = lister;
        return this;
    }

    public interface ActionLister {
        void onCheckClick(boolean isCheck);

        void onLeftClick(Dialog dialog);

        void onRightClick(Dialog dialog);
    }

    /**
     * 获取屏幕的宽度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    @Override
    public void dismiss() {
        checkBinding = null;
        super.dismiss();
    }
}